﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace T8task2
{
    public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        public Form1()
        {
            InitializeComponent();
        }

        private void ConnDatabase()
        {
            string sql = "select * from Categories;select * from Products";
            ds = DBHelper.DT(sql);

        }
        private void ShowCategoryList()
        {
            DataTable dt = ds.Tables[0];
            TreeNode root = new TreeNode("游戏类别");
            root.Tag = 0;
            treeView1.Nodes.Add(root);
            foreach (DataRow row in dt.Rows)
            {
                string name = Convert.ToString(row["Name"]);
                TreeNode node = new TreeNode(name);
                node.Tag = Convert.ToInt32(row["ID"]);
                root.Nodes.Add(node);
            }
            treeView1.ExpandAll();
        }

        private void WholeGame()
        {
            DataTable dt = ds.Tables[1];
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt;
        }
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;
            int id = Convert.ToInt32(node.Tag);
            if (id != 0)
            {
                DataView dv = ds.Tables[1].DefaultView;
                dv.RowFilter = string.Format("Categoryid={0}", id);
                dataGridView1.DataSource = dv;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ConnDatabase();
            ShowCategoryList();
            WholeGame();
        }
    }
}
